package com.zhihuichongcheng.application.ui.task.adapter.holder;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.smartlib.cmnObject.util.DialogUtil;
import com.smartlib.cmnObject.util.ToastOpt;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.task.TaskBean;
import com.zhihuichongcheng.application.constant.NetworkAPI;
import com.zhihuichongcheng.application.net.HttpCallback;
import com.zhihuichongcheng.application.ui.task.TaskDetailActivity;
import com.zhihuichongcheng.application.util.BroadCastUtils;

import java.util.HashMap;

/**
 * task holder Created by LK on 2017/1/5.
 */
public class TaskHolder extends BaseHolder {
    private ImageView mImgTaskPriority;
    private TextView mTaskTitle, mTaskFinishTime, mSubTaskCount, mTaskType;
    private TaskBean mTaskBeam;

    public TaskHolder(View itemView, BaseActivity activity) {
        super(itemView, activity);
        mImgTaskPriority = (ImageView) itemView.findViewById(R.id.img_task_priority);

        mTaskTitle = (TextView) itemView.findViewById(R.id.tv_title);
        mTaskFinishTime = (TextView) itemView.findViewById(R.id.tv_time);
        mSubTaskCount = (TextView) itemView.findViewById(R.id.tv_sub_task_count);
        mTaskType = (TextView) itemView.findViewById(R.id.task_type);
    }


    public void setData(final TaskBean taskBean, int position) {
        mTaskBeam = taskBean;
        if (taskBean.getPriorityName().equals("普通"))
            mImgTaskPriority.setImageResource(R.drawable.ic_task_lv_selector_3);
        else if (taskBean.getPriorityName().equals("紧急"))
            mImgTaskPriority.setImageResource(R.drawable.ic_task_lv_selector_2);
        else if (taskBean.getPriorityName().equals("非常紧急"))
            mImgTaskPriority.setImageResource(R.drawable.ic_task_lv_selector_1);
        mTaskTitle.setText(taskBean.getTitle());
        mTaskFinishTime.setText(taskBean.getSimpleFinishTime());
        mSubTaskCount.setText(taskBean.getSubTaskFinish() + "/" + taskBean.getSubTaskCount());
        mTaskType.setText(taskBean.getTypeName());

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt(TaskDetailActivity.EXTRA_TASK_ID, taskBean.getTaskId());
                startActivity(TaskDetailActivity.class, bundle);
            }
        });
        itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (taskBean.getCreateUserId() == Integer.parseInt(app.getUser().getUserId())) {
                    new MaterialDialog.Builder(itemView.getContext())
                            .title("请选择")
                            .items(taskBean.isLive() ? "结束任务" : "重启任务")
                            .itemsCallback(new MaterialDialog.ListCallback() {
                                @Override
                                public void onSelection(MaterialDialog dialog, View itemView, int position, CharSequence text) {
                                    if (taskBean.isLive())
                                        closeTask(position);
                                    else
                                        openTask(position);
                                }
                            })
                            .show();
                }
                return true;
            }
        });

        setItemStatus();
    }

    private void closeTask(int position) {
        DialogUtil.showLoadDiadlog(getContext());
        HashMap<String, String> paramsDemo = new HashMap<>();
        paramsDemo.put("token", mUser.getToken());
        paramsDemo.put("userMissionId", mTaskBeam.getTaskId() + "");
        manager.post(NetworkAPI.MISSION_FINISH, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(getContext());
                if (code == 2) {
                    ToastOpt.createToast(getContext(), msg);
                    return;
                }
                BroadCastUtils.getInstance().actionRefreshTaskList(getContext());
            }

            @Override
            public void onFailure(String value) {
                ToastOpt.createToast(getContext(), "关闭失败 " + value);
                DialogUtil.dismiss(getContext());
            }
        }, paramsDemo);
    }

    private void openTask(int position) {
        DialogUtil.showLoadDiadlog(getContext());
        HashMap<String, String> paramsDemo = new HashMap<>();
        paramsDemo.put("token", mUser.getToken());
        paramsDemo.put("userMissionId", mTaskBeam.getTaskId() + "");
        manager.post(NetworkAPI.MISSION_RESTART, new HttpCallback() {
            @Override
            public void onSuccess(int code, String value, String msg) {
                DialogUtil.dismiss(getContext());
                if (code == 2) {
                    ToastOpt.createToast(getContext(), msg);
                    return;
                }
                BroadCastUtils.getInstance().actionRefreshTaskList(getContext());
            }

            @Override
            public void onFailure(String value) {
                ToastOpt.createToast(getContext(), "重启失败 " + value);
                DialogUtil.dismiss(getContext());
            }
        }, paramsDemo);
    }

    private void setItemStatus() {
        mImgTaskPriority.setEnabled(mTaskBeam.isLive());
        mTaskTitle.setEnabled(mTaskBeam.isLive());
        mTaskFinishTime.setEnabled(mTaskBeam.isLive());
        mSubTaskCount.setEnabled(mTaskBeam.isLive());
        mTaskType.setEnabled(mTaskBeam.isLive());
    }

}
